<?php
/**
 * LoveScoreLogService.php
 * 文件描述 恩爱值账户记录
 * Created on 2020/8/31 18:35
 * Created by 刘小龙 595929049@qq.com
 */

namespace app\xiangji\services;



use app\common\facades\auth\Auth;
use app\common\models\LoveAchieve;
use app\common\models\User;
use app\xiangji\exception\BadRequestException;
use app\xiangji\exception\ModelNotFoundException;
use app\xiangji\exception\ServiceUnavailableException;
use think\facade\Db;

class LoveAchieveService
{
    const REWARD = [
        // 签到奖励 level => reward
        LoveAchieve::TYPE_SIGN => [
            3   => 5,
            7   => 10,
            11  => 15,
            15  => 20,
            19  => 30,
            23  => 40,
            27  => 50,
            31  => 60,
            35  => 70
        ],
        // 日记
        LoveAchieve::TYPE_DIARY => [
            1   => 5,
            5   => 10,
            10  => 20,
            15  => 40,
            20  => 50,
            25  => 100,
            30  => 200
        ],
        // 纪念日
        LoveAchieve::TYPE_ANNIVERSARY => [
            5   => 5,
            10  => 10,
            15  => 20,
            20  => 40,
            25  => 50,
            30  => 100
        ]
    ];

    private $user;

    public function __construct()
    {
        $this->user = Auth::guard('api')->user();
    }

    /**
     * 添加成就
     * @param User $user    用户对象
     * @param int $type     成就类型
     * @param int $level    成就等级
     * @return false|\think\Model|void
     */
    public static function addAchieve(User $user, int $type = LoveAchieve::TYPE_SIGN, int $level = 0)
    {
        if (!isset(self::REWARD[$type][$level])) return ; // 无该等级成就返回
        return $user->achieves()->save([
            'type'      => $type,
            'level'     => $level,
            'reward'    => self::REWARD[$type][$level],
            'status'    => 1
        ]);
    }

    /**
     * 查询用户成就
     * @param int $type 成就类型
     * @return array
     */
    public function queryAchieve(int $type)
    {
        if (!$type) return [];
        return $this->user->achieves()->where('type', '=', $type)->field(['id', 'type', 'level', 'reward', 'status'])->select();
    }

    /**
     * @param int $id   成就ID
     * @return bool
     * @throws BadRequestException
     * @throws ModelNotFoundException
     * @throws ServiceUnavailableException
     */
    public function acceptAchieveReward(int $id)
    {
        $achieve = $this->user->achieves()->find($id);
        if (!$achieve){
            throw new ModelNotFoundException('成就奖励信息错误');
        }
        if ($achieve->status > 1){
            throw new BadRequestException('已领取过该成就奖励');
        }
        Db::startTrans();
        try {
            LoveScoreService::updateUserScore($this->user->loveScore, $achieve->reward, '成就奖励');
            $achieve->save(['status' => 2]);
            Db::commit();
        } catch (\Exception $e){
            Db::rollback();
            throw new ServiceUnavailableException('系统错误，请稍后再试');
        }
        return true;
    }
}
